"""
used to create the response data of json type
"""
import json
from django.http import HttpResponse
from datetime import date, datetime

class ComplexEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)


class Resp(object):
    @classmethod
    def Error(self, msg: str, code: int) -> json:
        resp = {
            'rescode': code,
            'msg': msg
        }
        return json.dumps(resp,cls=ComplexEncoder)

    @classmethod
    def Right(self, data: object) -> json:
        resp = {
            'rescode': 200,
            'data': data
        }
        return json.dumps(resp,cls=ComplexEncoder)

    @classmethod
    def RightResp(self, data: json) -> json:
        return HttpResponse(
            json.dumps(data,cls=ComplexEncoder),
            content_type='application/json,charset=utf-8',
            status=200,
        )

    @classmethod
    def ErrorResp(self, data: json, status=500) -> json:
        return HttpResponse(
            json.dumps(data,cls=ComplexEncoder),
            content_type='application/json,charset=utf-8',
            status=status,
        )
